package com.isoftstone.bank.controller;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.bank.dao.RptMapper;
import com.isoftstone.bank.entity.BOrg;
import com.isoftstone.bank.service.impl.BOrgService;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.payment.Payment;
import com.isoftstone.merchant.service.payment.PaymentService;
import com.isoftstone.util.Const;
import com.isoftstone.util.ObjectExcelView;
import com.isoftstone.util.PageData;
import com.isoftstone.util.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wlj on 2017/12/19.
 * 银行端交易管理
 */
@Controller
public class DealController extends BaseController {
    @Autowired
    PaymentService paymentService;
    @Autowired
    private RptMapper mapper;
    @Autowired
    private BOrgService service;

    @RequiresPermissions("bms-menu.005.003")
    @RequestMapping("bank/toDeal")
    public ModelAndView toDeal(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("bank/deal/deal_inquiry");

        return mv;
    }
    /*
     * 交易查询列表
     * */
    @RequestMapping("/bank/deal/getFeeUserList")
    @ResponseBody
    public Object getFeeUserList(Page page) {
        Tidings<List<Payment>> tidings=new Tidings<List<Payment>>();
        PageData pd = this.getPageData();
//        if(null != pd.get("orgIds") && !"".equals(pd.get("orgIds"))){
//            String tem = pd.get("orgIds").toString();
//            if(",".equals(tem.substring(tem.length()-1,tem.length()))){
//                pd.put("orgIds", tem.substring(0,tem.length()-1));
//            }
//        }
        if(pd.get("orgId") == null || StringUtils.isEmpty(pd.get("orgId")+"")){
            pd.put("orgId",this.getUser().getCorpId());
        }
        BOrg org = service.selectByPrimaryKey(Long.valueOf(pd.get("orgId").toString()));
        pd.put("orgLevel",org.getOrgLevel());
        page.setPd(pd);

        this.getSession().setAttribute("corpId",pd.get("corpId"));
        this.getSession().setAttribute("orgId",pd.get("orgId"));
//        this.getSession().setAttribute("orgIds",pd.get("orgIds"));
        this.getSession().setAttribute("endDate",pd.get("endDate"));
        this.getSession().setAttribute("beginDate",pd.get("beginDate"));
        this.getSession().setAttribute("payStatus",pd.get("payStatus"));

        List<Payment> parmentList = new ArrayList<Payment>();
        try {
//            parmentList = paymentService.getmFeeUserlistPage(page);
            parmentList = mapper.getDeallistPage(page);
            tidings.setT(parmentList).setPage(page);
            tidings.setStatus(Const.SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            tidings.setStatus(Const.ERROR);
        }
        return tidings;
    }

    /*
* 批量导出
* */
    @RequestMapping("/bank/deal/exportToExcel")
    public ModelAndView exportToExcel(Page page) {
        ModelAndView mv = new ModelAndView();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<String> titles = new ArrayList<String>();
        titles.add("缴费流水号");
        titles.add("缴费人员");
        titles.add("机构");
        titles.add("收费项目");
        titles.add("应缴金额");
        titles.add("实缴金额");
        titles.add("支付流水号");
        titles.add("缴费时间");
        titles.add("缴费状态");
        titles.add("缴费方式");
        titles.add("收费人");
        dataMap.put("titles", titles);
        List<PageData> subList = new ArrayList<PageData>();
        PageData pd = this.getPageData();

        pd.put("corpId",this.getSession().getAttribute("corpId"));
        pd.put("orgId",this.getSession().getAttribute("orgId"));
        pd.put("endDate",this.getSession().getAttribute("endDate"));
        pd.put("beginDate",this.getSession().getAttribute("beginDate"));
        pd.put("payStatus",this.getSession().getAttribute("payStatus"));
        page.setPd(pd);
        List<Payment> parmentList = new ArrayList<Payment>();
        try {
            page.setShowCount(999999999);//mark
            parmentList = mapper.getDeallistPage(page);
            for (Payment payment : parmentList) {
                PageData subpd = new PageData();
                if(payment.getPayId() != null){//缴费流水号
                    subpd.put("var1", payment.getPayId().toString());
                }else{
                    subpd.put("var1","");
                }
                subpd.put("var2", payment.getFeeUserName());
                subpd.put("var3", payment.getDeptName());//机构
                subpd.put("var4", payment.getProjectName());

                if(payment.getTotalAmt() != null){//应缴
                    subpd.put("var5", payment.getTotalAmt().toString());
                }else {
                    subpd.put("var5", "");
                }

                if(payment.getFactAmt() != null){//实缴
                    subpd.put("var6", payment.getFactAmt().toString());
                }else{
                    subpd.put("var6", "");
                }
                subpd.put("var7", payment.getPaySeq());//支付流水号

                if(payment.getPayDate() != null){//缴费日期
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String payDate = sdf.format(payment.getPayDate());
                    subpd.put("var8", payDate);
                }else{
                    subpd.put("var8", "");
                }
                if (StringUtils.isNotBlank(payment.getPayStatus()) && payment.getPayStatus().equals("04")) {
                    subpd.put("var9", "待缴费");
                }
                if (StringUtils.isNotBlank(payment.getPayStatus()) && payment.getPayStatus().equals("P-00")) {
                    subpd.put("var9", "已缴费");
                }
                if (StringUtils.isNotBlank(payment.getPayType()) && payment.getPayType().equals("01")) {
                    subpd.put("var10", "POS");
                }else if (StringUtils.isNotBlank(payment.getPayType()) && payment.getPayType().equals("02")) {
                    subpd.put("var10", "现金");
                }else{
                    subpd.put("var10", "");
                }
                subpd.put("var11", payment.getUpdator());//收费人
                subList.add(subpd);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        dataMap.put("varList", subList);
        dataMap.put("name", "paymentHistory_");
        dataMap.put("sheetName", "历史交易记录查询");
        ObjectExcelView oev = new ObjectExcelView();
        mv = new ModelAndView(oev, dataMap);
        return mv;
    }

}
